var vm_jintiebofu = new Vue({
    el : "#jintiebofu",
    data : {
        deptid : $("#deptid").val(),		// 单位ID

        zjzxpiciid:'',

        jingfeipicilist : [],//批次列表
        jingfeipiciid : '',//批次ID
        jingfeipici : {},//批次对象

        //编辑标记
        editflag : '',

        monthlist:[],
        kaishinian:'',
        kaishiyue:'',
        jieshunian:'',
        jieshuyue:'',

        //查询用字段
        picimingcheng : '',

        //发放
        deptlist:[], //发放单位list
        deptids:'', //勾选发放单位ids
        zhuanjialist:[], //发放专家list
        zhuanjiaids:'' ,//勾选发放专家ids

        jisuanList:[],
        countjindu:0,
        countZhengchang:'',

    },
    created : function(){
        var _this = this;

        _this.bindJingfeiPiciList();

        if (_this.deptid=='000'){
            $("#shoukuantab").hide();
        }

        //_this.findDeptByParentId();
    },
    methods : {

        // 根据单位ID查询下级单位
        bindJingfeiPiciList : function(){
            var _this = this;

            var jingfeipici_layer = layer.open({type:3});
            $.post('/system/jingfeipici/customlist', {
                deptid : _this.deptid,
                piciname : _this.picimingcheng,
                zhuangtai : '0',
                rdm : Math.random()
            },function(ppData) {
                layer.close(jingfeipici_layer);

                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.jingfeipicilist = data;
                    }else{
                        layer.alert(ppData.msg);
                    }
                }
            },"json");
        },

        getJifanianyue:function(){
            var _this = this;

            _this.kaishinian=$("#kaishinian").val();
            _this.kaishiyue=$("#kaishiyue").val();
            _this.jieshunian=$("#jieshunian").val();
            _this.jieshuyue=$("#jieshuyue").val();
        },

        getJiezhishijian:function(){
            var _this = this;

            _this.jingfeipici.jiezhishijian=$("#jiezhishijian").val();
        },

        getMonth:function () {
            $("#monthselect").empty();
            $('#monthselect').selectpicker('val',['noneSelectedText'])
            $('#monthselect').selectpicker('refresh');
            var _this = this
            _this.monthlist = []
            $.ajaxSettings.async = false;
            $.post('/system/jingfeideptmonth/findnotusedmonth', {
                deptid: _this.deptid,
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == 1) {
                        _this.monthlist = ppData.resultContent;
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
            $.ajaxSettings.async = true;
            if (_this.monthlist && _this.monthlist.length != 0) {
                var options = "";
                for (var i = 0; i < _this.monthlist.length; i++) {
                    options += "<option "+  "month= '"+ _this.monthlist[i].shoukuannianyue +"'" +
                        " value='" + _this.monthlist[i].id + " '>"+
                        '('+ _this.monthlist[i].name+')' + _this.monthlist[i].shoukuannianyue + "</option>";
                }

                $('#monthselect').append(options);
            }
            console.log(_this.monthlist)
            $('#monthselect').selectpicker('refresh');
        },
        toAdd:function(){
            this.getMonth()
            this.jingfeipici = {};
            this.editflag = 1;
            $("#myModalLabel").html("新增经费批次");
            $("#modal-dialog").attr("style","width:1200px;");
            $("#jingfeipiciModal").modal();
        },

        //
        add : function(){
            var _this = this;
            var shoukuanListtmp=[]
            var kaishinianyue =new Date(_this.kaishinian+"-"+_this.kaishiyue);
            var jieshunianyue =new Date(_this.jieshunian+"-"+_this.jieshuyue);


            if(_this.deptid=="000") {
                for (var i = kaishinianyue.getMonth(); i < jieshunianyue.getMonth() + 1; i++) {
                    shoukuanListtmp.push({shoukuannianyue: new Date(kaishinianyue.setMonth(i)).toLocaleDateString('zh').replaceAll('/', '-')})
                }
            }else {
                var checkParam = $('#monthselect').find('option:selected');
                for (var i=0;i<checkParam.length;i++) {
                    shoukuanListtmp.push({
                        shoukuannianyue: checkParam[i].getAttribute('month'),
                        id:checkParam[i].value
                    })
                }
            }
            var param = {
                ex_deptid : _this.deptid,
                ex_picimingcheng : _this.jingfeipici.picimingcheng,
                ex_kaishinian : _this.kaishinian,
                ex_kaishiyue : _this.kaishiyue,
                ex_jieshunian : _this.jieshunian,
                ex_jieshuyue : _this.jieshuyue,
                ex_jiezhishijian : _this.jingfeipici.jiezhishijian,
                jingfeiDeptMonthList :shoukuanListtmp,
                ZjzxpiciId : _this.zjzxpiciid,
                rdm : Math.random()
            }

            if (_this.checkInputData()) {
                var jingfeipici_layer = layer.open({type:3});

                $.ajax({
                    url: "/system/jingfeipici/customadd",
                    type: "POST",
                    data: JSON.stringify(param),
                    dataType: "json",
                    contentType: "application/json",
                    success: function (ppData) {
                        layer.close(jingfeipici_layer);
                        if (ppData != null) {
                            if (ppData.result == "1"){

                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: "添加成功!",
                                });

                                _this.bindJingfeiPiciList();
                                $("#jingfeipiciModal").modal('hide');
                            }else{
                                layer.alert(ppData.message);
                            }
                        }}
                });
            }
        },

        Delete:function(jingfeipiciitem){
            var _this = this;

            $.modal.confirm("是否确认删除:"+jingfeipiciitem.picimingcheng,function(){
                $.post('/system/jingfeipici/customremove', {
                    piciid : jingfeipiciitem.jingfeipiciid,
                    rdm : Math.random()
                },function(ppData) {

                    if(ppData != null){

                        if(ppData.result == "1"){
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "删除成功!",
                            });
                            _this.bindJingfeiPiciList();
                        }else{
                            layer.alert(ppData.message);
                        }
                    }
                },"json");
            });
        },

        //检查项目数据
        checkInputData : function() {
            var _this = this;
            if(this.deptid=="000"){
                if (!$.trim(this.jingfeipici.picimingcheng)) {
                    layer.alert("请填写拨款批次！");
                    return false;
                }

                if (!$.trim(this.kaishinian)) {
                    layer.alert("请选择计发年月！");
                    return false;
                }

                if (!$.trim(this.kaishiyue)) {
                    layer.alert("请选择计发年月！");
                    return false;
                }

                if (!$.trim(this.jieshunian)) {
                    layer.alert("请选择计发年月！");
                    return false;
                }

                if (!$.trim(this.jieshuyue)) {
                    layer.alert("请选择计发年月！");
                    return false;
                }
                var kaishinianyue =new Date(_this.kaishinian+"-"+_this.kaishiyue);
                var jieshunianyue =new Date(_this.jieshunian+"-"+_this.jieshuyue);
                if(kaishinianyue>=jieshunianyue){
                    layer.alert("起始年月不能大于结束年月")
                    return false;
                }
                if (!$.trim(this.jingfeipici.jiezhishijian)) {
                    layer.alert("请选择本批次拨款截止时间！");
                    return false;
                }
            }else {
                if (!$.trim(this.jingfeipici.picimingcheng)) {
                    layer.alert("请填写拨款批次！");
                    return false;
                }
                if (!$.trim(this.jingfeipici.jiezhishijian)) {
                    layer.alert("请选择本批次拨款截止时间！");
                    return false;
                }
                if ($("#monthselect").val().length==0) {
                    layer.alert("请选择计发年月！");
                    return false;
                }
            }

            return true;
        },

        reset:function () {
            this.picimingcheng=""
            this.bindJingfeiPiciList()
        },

        Export:function(jingfeipiciitem){
            var _this = this;
            location.href = "/export/daochu_bokuanbiao?piciid="+jingfeipiciitem.id
        },

        //
        toDanweiqueren : function(jingfeipiciitem){
            $.modal.openTab("单位确认情况",'system/jintieguanli/danweiqueren?jingfeipiciid='+jingfeipiciitem.jingfeipiciid
                +"&deptid="+jingfeipiciitem.deptid
                +"&jiezhishijian="+jingfeipiciitem.jiezhishijian
                ,false);
        },

        findDeptByParentId:function(){
            var _this = this;

            $.post('/system/deptdetail/find_by_parentid', {
                deptid : _this.deptid,
                rdm : Math.random()
            },function(ppData) {
                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.jisuanList = data;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        countJindu:function(ppPiciId){
            var _this = this;

            $.post('/system/jingfeipici/chaxunjindu', {
                piciid : ppPiciId,
                deptid : _this.deptid,
                rdm : Math.random()
            },function(ppData) {
                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.countjindu = data;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },


        toJisuan : function(jingfeipiciitem){
            var _this = this;

            $("#jinduModal").modal();

            // var timeout = setTimeout(function(){
            //     layer.alert("请求超时");
            // },600000);

            $.post('/system/jingfeipici/zjzxjingfeijisuan', {
                piciid: jingfeipiciitem.jingfeipiciid,
                rdm: Math.random()
            }, function (ppData) {

                // if(timeout){
                //     clearTimeout(timeout);
                //     timeout=null;
                // }

                if (ppData != null) {

                    if (ppData.result == "1") {

                        $("#jinduModal").modal("hide");
                        layer.closeAll("loading");
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "计算成功!",
                        });

                        _this.bindJingfeiPiciList();

                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");

        },

        toDeptJisuan : function(jingfeipiciitem){
            var _this = this;

            $("#jinduModal").modal();

            // var timeout = setTimeout(function(){
            //     layer.alert("请求超时");
            // },600000);

            $.post('/system/jisuan/deptjingfeijisuan', {
                piciid: jingfeipiciitem.jingfeipiciid,
                rdm: Math.random()
            }, function (ppData) {

                // if(timeout){
                //     clearTimeout(timeout);
                //     timeout=null;
                // }

                if (ppData != null) {

                    if (ppData.result == "1") {

                        $("#jinduModal").modal("hide");
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "计算成功!",
                        });

                        _this.bindJingfeiPiciList();

                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");

        },

        // 发放查询单位列表
        bindDeptList : function(jingfeipiciId){
            var _this = this;

            $.post('/system/jisuan/fafang_list', {
                piciid:jingfeipiciId,
                rdm : Math.random()
            },function(ppData) {
                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.deptlist = data.deptlist;
                        _this.zhuanjialist = data.zhuanjialist;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        //发放经费
        toFafangJingfei: function (jingfeipiciitem) {
            var _this = this
            _this.jingfeipiciid = jingfeipiciitem.jingfeipiciid;
            _this.bindDeptList(jingfeipiciitem.jingfeipiciid)
            $("#fafangLabel").html("津贴发放");
            $("#fafangmodal-dialog").attr("style","width:1500px;");
            $("#fafangModal").modal();
        },

        //发放经费
        FafangJingfei: function () {
            var _this = this;
            var chk_value_dept =[];
            var chk_value_zhuanjia =[];

            _this.deptids = '';
            _this.zhuanjiaids = '';

            $('input[name="dept"]:checked').each(function(){
                chk_value_dept.push($(this).val());
            });
            $('input[name="zhuanjia"]:checked').each(function(){
                chk_value_zhuanjia.push($(this).val());
            });
            if (chk_value_dept.length==0&&chk_value_zhuanjia.length==0){
                layer.alert('您还没有选择任何单位或专家！');
                return
            }else {
                for (var i = 0; i < chk_value_dept.length; i++) {
                    for (var j = 0; j < _this.deptlist.length; j++) {
                        if (chk_value_dept[i] == _this.deptlist[j].deptid) {
                            _this.deptids = _this.deptids + _this.deptlist[j].deptid + ",";
                        }
                    }
                }
                for (var i = 0; i < chk_value_zhuanjia.length; i++) {
                    for (var j = 0; j < _this.zhuanjialist.length; j++) {
                        if (chk_value_zhuanjia[i] == _this.zhuanjialist[j].zhuanjiaid) {
                            _this.zhuanjiaids = _this.zhuanjiaids + _this.zhuanjialist[j].zhuanjiaid + ",";
                        }
                    }
                }
            }
            $.post('/system/jisuan/fafang', {
                piciid : _this.jingfeipiciid,
                deptids: _this.deptids,
                zhuanjiaids: _this.zhuanjiaids,
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == "1") {
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "发放成功!",
                        })
                        _this.bindJingfeiPiciList()
                        $("#fafangModal").modal('hide');
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        //发放经费
        DeptFafangJingfei: function () {
            var _this = this;
            var chk_value_dept =[];
            var chk_value_zhuanjia =[];

            _this.deptids = '';
            _this.zhuanjiaids = '';

            $('input[name="dept"]:checked').each(function(){
                chk_value_dept.push($(this).val());
            });
            $('input[name="zhuanjia"]:checked').each(function(){
                chk_value_zhuanjia.push($(this).val());
            });
            if (chk_value_dept.length==0&&chk_value_zhuanjia.length==0){
                layer.alert('您还没有选择任何单位或专家！');
                return
            }else {
                for (var i = 0; i < chk_value_dept.length; i++) {
                    for (var j = 0; j < _this.deptlist.length; j++) {
                        if (chk_value_dept[i] == _this.deptlist[j].deptid) {
                            _this.deptids = _this.deptids + _this.deptlist[j].deptid + ",";
                        }
                    }
                }
                for (var i = 0; i < chk_value_zhuanjia.length; i++) {
                    for (var j = 0; j < _this.zhuanjialist.length; j++) {
                        if (chk_value_zhuanjia[i] == _this.zhuanjialist[j].zhuanjiaid) {
                            _this.zhuanjiaids = _this.zhuanjiaids + _this.zhuanjialist[j].zhuanjiaid + ",";
                        }
                    }
                }
            }
            $.post('/system/jisuan/deptfafang', {
                piciid : _this.jingfeipiciid,
                deptids: _this.deptids,
                zhuanjiaids: _this.zhuanjiaids,
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == "1") {
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "发放成功!",
                        })
                        _this.bindJingfeiPiciList()
                        $("#fafangModal").modal('hide');
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        //
        toJingfeiPiciDetail : function(jingfeipiciitem){
            $.modal.openTab("单位拨款详情",'system/jintieguanli/bokuandetail?jingfeipiciid='+jingfeipiciitem.jingfeipiciid+"&deptid="+jingfeipiciitem.deptid,false);
        },

        toShoukuan: function () {
            $.modal.parentTab("津贴收款","/system/jintieguanli/jintieshoukuan");
        },

        toBofu: function () {
            $.modal.parentTab("津贴拨付","/system/jingfeipici/dangpicibofu");
        },

        //全选按钮的点击事件
        checkAll1(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll1');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne1(){
            let count=0;
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll1').checked = true;
            }else {
                document.getElementById('allAndNotAll1').checked = false;
            }
        },

        //全选按钮的点击事件
        checkAll2(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll2');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne2(){
            let count=0;
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll2').checked = true;
            }else {
                document.getElementById('allAndNotAll2').checked = false;
            }
        },

    },
})